#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Carte.h"
#include <iostream>

using namespace std;

Carte::Carte(int taille, int** decore, int nbDeco):_taille(taille),_nbDeco(nbDeco){
	int i = 0;
	_decore = decore;
	srand(time(NULL));
}

int** Carte::creerCarte() {
	int i = 0;
	int j = 0;
	int randNb = 0;
	int randNb2;
	int randNb3;
	int check[]={0,0,0,0,0,0,0,0,0};
	int** tab = new int*[_taille];
	//*tab = new int [_taille];
	bool b = true;
	while(b){
		b = false;
		for(i=0 ; i<_taille ; i+=2){
			tab[i] = new int[_taille];
			tab[i+1] = new int[_taille];
			for(j=0 ; j<_taille ; j++){
				randNb = rand() % 3;
				if((i+1) < _taille){
					if((j+1) < _taille) {
						tab[i][j]=randNb;
						tab[i+1][j]=randNb;
						tab[i][j+1]=randNb;
						tab[i+1][++j]=randNb;
					}
					else{
						tab[i][j]=tab[i][j-1];
						tab[i+1][j]=tab[i+1][j-1];
					}
				}
				else{
					if((j+1) < _taille) {
						tab[i][j]=tab[i-1][j];
						tab[i][j+1]=tab[i-1][j];

					}
					else{
						tab[i][j]=tab[i-1][j-1];
					}
				}
				check[randNb]+=1;
			}
		}

		for(i=0;i<_taille/2;i++){
			randNb2=rand()%_taille;
			randNb3=rand()%_taille;
			randNb=rand()%2;
			int tmp = tab[randNb2][randNb3];
			if(tmp<3)
			{
				tab[randNb2][randNb3]= _decore[tmp][randNb];
				tmp = tab[randNb2][randNb3];
				check[tmp] +=1;
			}
		}



		for(int k=0 ; k < 9 ; k++){
			if(check[k]==0){
				b = true;
			}
		}
	}
	_carte = tab;
	return tab;
}

int** Carte::suggestion(int** tab, int* ressources){
	int i = 0;
	int j = 0;
	int k = 0;
	int l = 0;
	int** temp;
	temp = (int**)malloc(_taille*sizeof(*tab));
	for(int i = 0 ; i < _taille ; i ++){
		temp[i] = (int*) malloc(_taille*sizeof(*temp[i]));
	}

	int** sug = new int*[3];
	sug[0] = new int[2];
	sug[1] = new int[2];
	sug[2] = new int[2];
	
	int ress[] = {0,0,0};
	for(i=0 ; i < _taille ; i++){
		for(j=0 ; j < _taille ; j++){
			if(ressources[i*_taille+j]>3){
				int x = 0;
				if(tab[i][j]==1){
					temp[i][j]=0;
				} else if(tab[i][j]==2){

					for(k=i-2; k <i+3 && k < _taille ; k++){
						for(l=j-2; l <j+3 && l < _taille ; l++){
							if(k>=0 && l >=0){
								temp[i][j] = 0;
							}
						}
					}

				} else {
					for(k=i-2; k <i+3 && k < _taille ; k++){
						for(l=j-2; l <j+3 && l < _taille ; l++){
							if(k>=0 && l >=0){
								x+=ressources[k*_taille+l];
							}
						}
					}

					temp[i][j]=x;
				}

				if(temp[i][j] > ress[0]){
					ress[0]=temp[i][j];
					sug[0][0]=i;
					sug[0][1]=j;
				}else if(temp[i][j] > ress[1]){
					ress[1]=temp[i][j];
					sug[1][0]=i;
					sug[1][1]=j;
				}else if(temp[i][j] > ress[2]){
					ress[2]=temp[i][j];
					sug[2][0]=i;
					sug[2][1]=j;
				}
			}
		}
	}
	return sug;
}
//int** Carte::suggestion(int** tab, int* ressources){
//	int i = 0;
//	int j = 0;
//	int k = 0;
//	int l = 0;
//	//int** temp = new int*[_taille];
//	int** temp;
//
//		
//	temp = (int**)malloc(_taille*sizeof(*tab));
//
//	for(int i = 0 ; i < _taille ; i ++){
//		temp[i] = (int*) malloc(_taille*sizeof(*temp[i]));
//	}
//
//	int** sug = new int*[3];
//	sug[0] = new int[2];
//	sug[1] = new int[2];
//	sug[2] = new int[2];
//	
//	int ress[] = {0,0,0};
//	for(i=0 ; i < _taille ; i++){
//		for(j=0 ; j < _taille ; j++){
//			if(tab[i][j]==1){
//				temp[i][j]=0;
//			} else if(tab[i][j]==2){
//
//				for(k=i-2; k <i+3 && k < _taille ; k++){
//					for(l=j-2; l <j+3 && l < _taille ; l++){
//						if(k >=0 && l >=0){
//							temp[i][j]=0;
//						}
//					}
//				}
//			} else {
//				int x = 0;
//
//				for(k=i-2; k <i+3 && k < _taille ; k++){
//					for(l=j-2; l <j+3 && l < _taille ; l++){
//						if(k >=0 && l >=0){
//							x+=ressources[k*_taille+l];
//						}
//					}
//				}
//
//				temp[i][j]=x;
//
//				if(temp[i][j] > ress[0]){
//					ress[0]=temp[i][j];
//					sug[0][0]=i;
//					sug[0][1]=j;
//				}else if(temp[i][j] > ress[1]){
//					ress[1]=temp[i][j];
//					sug[1][0]=i;
//					sug[1][1]=j;
//				}else if(temp[i][j] > ress[2]){
//					ress[2]=temp[i][j];
//					sug[2][0]=i;
//					sug[2][1]=j;
//				}
//			}
//		}
//	}
//
//	return sug;
//
//}

Carte::~Carte() {
	delete _decore;
}

Carte* Carte_new(int taille, int** decore, int nbDeco) { return new Carte(taille, decore, nbDeco); }
void Carte_delete(Carte* carte) { carte ->~Carte(); }
int** Carte_creerCarte(Carte* carte) { 
	return carte->creerCarte();
}
int** Carte_suggestion(Carte* carte, int** tab, int* ressources){
	return carte->suggestion(tab,ressources);
}

/*
int main() {
	int tab[6] = {2,3,4,6,7,8};
	int **t;
	int i,j=0;
	Carte c(25,tab,6);

	t = c.creerCarte();

	for(i=0;i<25;i++) {
		for(j=0;j<25;j++) {
			cout << "t["<< i << "]["<<j<<"] = "<<t[i][j] << endl;
		}
	}

	return 0;
}*/